﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;

namespace RuoVea.ExUtil;

/// <summary>
/// 描 述：扩展 - 可空类型
/// </summary>
public static partial class Extensions
{
    /// <summary>
    /// 安全返回值
    /// </summary>
    /// <typeparam name="T">泛型</typeparam>
    /// <param name="value">可空值</param>
    /// <returns>指定类型数据</returns>
    public static T SafeValue<T>(this T? value) where T : struct
    {
        return value ?? default(T);
    }
    /// <summary>
    /// 是否包含
    /// </summary>
    /// <param name="obj">字串</param>
    /// <param name="value">包含字串</param>
    /// <returns>是否包含</returns>
    public static bool ContainsEx(this string obj, string value)
    {
        if (string.IsNullOrEmpty(obj))
        {
            return false;
        }
        else
        {
            return obj.Contains(value);
        }
    }
    /// <summary>
    /// 字串是否在指定字串中存在
    /// </summary>
    /// <param name="obj">字串</param>
    /// <param name="value">被包含字串</param>
    /// <returns>是否在指定字串中存在</returns>
    public static bool Like(this string obj, string value)
    {
        if (string.IsNullOrEmpty(value))
        {
            return false;
        }
        else if (string.IsNullOrEmpty(obj))
        {
            return false;
        }
        else
        {
            if (value.IndexOf(obj) != -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

    /// <summary>
    /// 安全判断是空
    /// </summary>
    /// <param name="str">字串</param>
    /// <returns>安全判断是空</returns>
    public static bool IsNullOrWhiteSpace(this string str)
    {
        return string.IsNullOrWhiteSpace(str);
    }

    /// <summary>
    ///  安全判断不是空
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>安全判断不是空</returns>
    public static bool NotNullOrWhiteSpace(this string str)
    {
        return !string.IsNullOrWhiteSpace(str);
    }


    #region 扩展
    /// <summary>
    /// 安全判断是空
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>安全判断是空</returns>
    public static bool IsNullOrWhiteSpace(this int? str)
    {
        return str == null ? true : false;
    }
    /// <summary>
    /// 安全判断不是空
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>安全判断不是空</returns>
    public static bool NotNullOrWhiteSpace(this int? str)
    {
        return str == null ? false : true;
    }

    /// <summary>
    /// 安全判断是空
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>安全判断是空</returns>
    public static bool IsNullOrWhiteSpace(this int str)
    {
        return str == 0 ? true : false;
    }
    /// <summary>
    /// 安全判断不是空
    /// </summary>
    /// <param name="str">字符串</param>
    /// <returns>安全判断不是空</returns>
    public static bool NotNullOrWhiteSpace(this int str)
    {
        return str == 0 ? false : true;
    }
    #endregion

    /// <summary>
    /// 将字符串转换为int类型数组
    /// </summary>
    /// <param name="str">如1,2,3,4,5</param>
    /// <returns>int类型数组</returns>
    public static List<string> StrToListString(this string str)
    {
        var list = new List<string>();
        if (!str.Contains(","))
        {
            list.Add(str);
            return list;
        }
        var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var item in slist)
        {
            list.Add(item);
        }
        return list;
    }

    /// <summary>
    /// 去除重复列表数据
    /// </summary>
    /// <typeparam name="TSource">泛型</typeparam>
    /// <typeparam name="TKey">泛型</typeparam>
    /// <param name="source">资源</param>
    /// <param name="keySelector">方法</param>
    /// <returns>泛型列表数据</returns>
    public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        HashSet<TKey> seenKeys = new HashSet<TKey>();
        foreach (TSource element in source)
        {
            if (seenKeys.Add(keySelector(element)))
            {
                yield return element;
            }
        }
    }

    #region 删除最后一个字符之后的字符
    /// <summary>
    /// 删除最后结尾的一个逗号
    /// </summary>
    /// <param name="str">字串</param>
    /// <returns>字符</returns>
    public static string DelLastComma(this string str)
    {
        return str.Substring(0, str.LastIndexOf(","));
    }

    /// <summary>
    /// 删除最后结尾的指定字符后的字符
    /// </summary>
    /// <param name="str">字串</param>
    /// <param name="strchar">指定的字符</param>
    /// <returns>字符</returns>
    public static string DelLastChar(this string str, string strchar)
    {
        return str.Substring(0, str.LastIndexOf(strchar));
    }

    /// <summary>
    /// 删除最后结尾的长度
    /// </summary>
    /// <param name="str">字串</param>
    /// <param name="Length">删除长度</param>
    /// <returns>字符</returns>
    public static string DelLastLength(this string str, int Length)
    {
        if (string.IsNullOrEmpty(str))
            return "";
        str = str.Substring(0, str.Length - Length);
        return str;
    }
    #endregion

    /// <summary>
    /// 转换成地址参数
    /// </summary>
    /// <param name="source">属性</param>
    /// <returns>字符</returns>
    public static string ToParams(object source)
    {
        var stringBuilder = new StringBuilder(string.Empty);
        if (source == null)
        {
            return "";
        }
        var entries = from PropertyDescriptor property in TypeDescriptor.GetProperties(source)
                      let value = property.GetValue(source)
                      where value != null
                      select (property.Name, value);
        foreach (var (name, value) in entries)
            stringBuilder.Append(WebUtility.UrlEncode(name) + "=" + WebUtility.UrlEncode(value + "") + "&");

        return stringBuilder.ToString().Trim('&');
    }
}
